package com.yun.zhong.fu.codelianxi.controller;

import com.yun.zhong.fu.codelianxi.entity.PageInfo;
import com.yun.zhong.fu.codelianxi.service.ImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author RenPu
 * @Date 2021/1/18 14:20
 * @Version 1.0
 * @Description: 批量文件上传的控制层
 **/

@RequestMapping("/images")
public class ImageController {

    @Autowired
    private ImagesService imagesService;

    @RequestMapping(value = "/addMessage")
    public String addMessage(@RequestParam("file") MultipartFile[] file, HttpServletRequest request) {
        String messageInfo = request.getParameter("messageInfo");
        String userId = request.getParameter("userId");
        /*  String file = request.getParameter("file");*/
        List<MultipartFile> listfile = new ArrayList<>();
//        MultipartHttpServletRequest multipartHttpServletRequest=(MultipartHttpServletRequest)request;
        /* MultipartFile file1 = multipartHttpServletRequest.getFile(file);*/
        // 转换request，解析出request中的文件
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        // 获取文件map集合
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile mf = entity.getValue();
            listfile.add(mf);
        }

        return imagesService.addMessage(listfile, request);
    }


}
